package com.hotgirls;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.animation.AccelerateInterpolator;

import com.google.ads.AdView;


public class HotgirlsActivity extends Activity {

	//private ImageView imageView;
	private ImageView imgAnimation;
	private Button pre;
	private Button next;
	private RelativeLayout rlShow;
	private int id = 0;
	
	static final String logTag = "ActivitySwipeDetector";
	static final int MIN_DISTANCE = 100;
	private float downX, downY, upX, upY;
	int screenHeight;
	int screenWidth;
	
	private AdView adView;

	int Image[] = { R.drawable.pic_1, R.drawable.pic_2, R.drawable.pic_3,
			R.drawable.pic_4, R.drawable.pic_5, R.drawable.pic_6,
			R.drawable.pic_7, R.drawable.pic_8, R.drawable.pic_9,
			R.drawable.pic_10, R.drawable.pic_11, R.drawable.pic_12,
			R.drawable.pic_13, R.drawable.pic_14, R.drawable.pic_15,
			R.drawable.pic_16, R.drawable.pic_17, R.drawable.pic_18,
			R.drawable.pic_19, R.drawable.pic_20, R.drawable.pic_21,
			R.drawable.pic_22, R.drawable.pic_23, R.drawable.pic_24,
			R.drawable.pic_25, R.drawable.pic_26, R.drawable.pic_27,
			R.drawable.pic_28, R.drawable.pic_29, R.drawable.pic_30,
			R.drawable.pic_31, R.drawable.pic_32, R.drawable.pic_33,
			R.drawable.pic_34, R.drawable.pic_35, R.drawable.pic_36,
			R.drawable.pic_37, R.drawable.pic_38, R.drawable.pic_39,
			R.drawable.pic_40, R.drawable.pic_41, R.drawable.pic_42,
			R.drawable.pic_43, R.drawable.pic_44, R.drawable.pic_45,
			R.drawable.pic_46, R.drawable.pic_47, R.drawable.pic_48,
			R.drawable.pic_49, R.drawable.pic_50, R.drawable.pic_51,
			R.drawable.pic_52, R.drawable.pic_53, R.drawable.pic_54,
			R.drawable.pic_55, R.drawable.pic_56, R.drawable.pic_57,
			R.drawable.pic_58, R.drawable.pic_59, R.drawable.pic_60,
			R.drawable.pic_61, R.drawable.pic_62, R.drawable.pic_63,
			R.drawable.pic_64, R.drawable.pic_65, R.drawable.pic_66,
			R.drawable.pic_67, R.drawable.pic_68, R.drawable.pic_69,
			R.drawable.pic_70, R.drawable.pic_71, R.drawable.pic_72,
			R.drawable.pic_73, R.drawable.pic_74, R.drawable.pic_75,
			R.drawable.pic_76, R.drawable.pic_77, R.drawable.pic_78,
			R.drawable.pic_79, R.drawable.pic_80, R.drawable.pic_81,
			R.drawable.pic_82, R.drawable.pic_83, R.drawable.pic_84,
			R.drawable.pic_85, R.drawable.pic_86, R.drawable.pic_87,
			R.drawable.pic_88, R.drawable.pic_89, R.drawable.pic_90,
			R.drawable.pic_91, R.drawable.pic_92, R.drawable.pic_93,
			R.drawable.pic_94, R.drawable.pic_95, R.drawable.pic_96,
			R.drawable.pic_97, R.drawable.pic_98, R.drawable.pic_99 };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_hotgirls);

		// add adView

		/*
		 * DisplayMetrics metrics = new DisplayMetrics();
		 * getWindowManager().getDefaultDisplay().getMetrics(metrics); int
		 * screenWidth = (int) (metrics.widthPixels * metrics.density); int
		 * screenHeight = (int) (metrics.heightPixels * metrics.density);
		 */

		pre = (Button) findViewById(R.id.btnpreview);
		next = (Button) findViewById(R.id.btnnext);
		
		//join anh dong
		imgAnimation=(ImageView)findViewById(R.id.imgAnimation);
		imgAnimation.setBackgroundResource(R.drawable.bird1);
		setLayout(imgAnimation, -10, 0, 0, 20);

		rlShow = (RelativeLayout) findViewById(R.id.rlShow);

	    rlShow.setBackgroundResource(Image[id]);
		//imageView = (ImageView) findViewById(R.id.imageView);
		//imageView.setBackgroundResource(Image[id]);
	    animBird();
		pre.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (id > 0) {
					rlShow.setBackgroundResource(Image[id]);
					id--;
				}
			}
		});

		next.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (id < Image.length - 1) {
					rlShow.setBackgroundResource(Image[id]);
					id++;
				}
			}
		});

		// Xac dinh kich thuoc cua man hinh
				DisplayMetrics metrics = new DisplayMetrics();
				getWindowManager().getDefaultDisplay().getMetrics(metrics);
				screenWidth = (int) (metrics.widthPixels * metrics.density);
				screenHeight = (int) (metrics.heightPixels * metrics.density);
		
		// touch image
		rlShow.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				
				switch (event.getAction()) {
					case MotionEvent.ACTION_DOWN:
						downX = event.getX();
			            downY = event.getY();
						
						return true;
					case MotionEvent.ACTION_UP:
						upX = event.getX();
			            upY = event.getY();
	
			            float deltaX = downX - upX;
			            float deltaY = downY - upY;
			            
				         // swipe horizontal?
				        if(Math.abs(deltaX) > MIN_DISTANCE){
				                // left or right
				            	rlShow.setBackgroundResource(Image[id]);
				                if(deltaX < 0) { 
				                	if(id>0){
				                		id--;
				                	}			                	
				                	return true; 
				                }
				                if(deltaX > 0) {  
				                	if(id<Image.length)
				                	id++;			                	
				                	if(id==10){		                		 
			                		    Button button = new Button(getApplicationContext());
			                		    //button.setText("Next");
			                		    
			                		    android.widget.RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			                		    //params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
			                		    params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
			                		    params1.addRule(RelativeLayout.CENTER_VERTICAL);
			                		    //params1.setMargins(screenWidth-(int)button.getWidth(),screenHeight-50 , 0, 50);
			                		    button.setLayoutParams(params1);
			                		    rlShow.addView(button);	
			                		    
			                		    
			                		    //move button
			                		    if (button != null) {
			                				button.setBackgroundResource(R.anim.move_next);
			                				AnimationDrawable ani = (AnimationDrawable) button.getBackground();
			                				if (ani.isRunning()) {
			                					ani.stop();				
			                				} else {
			                					ani.stop();
			                					ani.start();
			                				}
			                			}
			                		    //ko cho touch nua
			                		    return true;
				                	}
				                	 
				                }
				        }
				        else {
				            Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
				            return false; // We don't consume the event
				        }
			            
						return true;
					
					default:
						break;
				}
				/*
				 * imageView.setBackgroundResource(Image[id]);
				 * if(id<Image.length-1){ id++; } else id--;
				 */
				//den anh thu 10 khong cho touch
				
				return false;
			}
		});
		//load anh dong
		//animBird();		
	}
	//cho nay dang sua file xml trong anim
	private void animBird() {
		if (imgAnimation != null) {
			imgAnimation.setBackgroundResource(R.anim.bird);
			AnimationDrawable ani = (AnimationDrawable) imgAnimation.getBackground();
			if (ani.isRunning()) {
				ani.stop();				
			} else {
				ani.stop();
				ani.start();
			}
		}
	}
	
	
	private void setLayout(View v, int left, int top, int right, int bottom) {
		RelativeLayout.LayoutParams privew = new RelativeLayout.LayoutParams(
				Math.round(v.getLayoutParams().width), Math.round(v
						.getLayoutParams().height));

		privew.setMargins(left, top, right, bottom);
		v.setLayoutParams(privew);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_hotgirls, menu);
		return true;
	}
		
}




